{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:03:30.198669Z",
     "start_time": "2025-02-15T12:03:30.186810Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from typing import List, Dict, Tuple, Any\n",
    "\n",
    "from user import User, UserState\n",
    "from edge import Edge\n",
    "\n",
    "import tools\n",
    "\n",
    "np.random.seed(2025)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:03:38.346005Z",
     "start_time": "2025-02-15T12:03:32.462305Z"
    }
   },
   "outputs": [],
   "source": [
    "library_size = 10000\n",
    "ndim = 20\n",
    "nuser = 80\n",
    "\n",
    "video_durations = np.random.randint(low=8, high=15, size=library_size, dtype=np.int16)\n",
    "video_features = np.array([tools.generate_feature_vector(ndim) for _ in range(library_size)])\n",
    "videos = { \"feature\": video_features, \"duration\": video_durations }\n",
    "\n",
    "user_preferences = np.array([tools.generate_feature_vector(ndim) for _ in range(nuser)])\n",
    "\n",
    "record_table = { uid: dict() for uid in range(nuser) }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:03:38.354785Z",
     "start_time": "2025-02-15T12:03:38.346005Z"
    }
   },
   "outputs": [],
   "source": [
    "users = []\n",
    "for i in range(nuser):\n",
    "    user = User(i, user_preferences[i], np.random.random(),videos, record_table[i], 1, False)\n",
    "    users.append(user)\n",
    "\n",
    "edge = Edge(videos, user_preferences, record_table, 600)\n",
    "\n",
    "for user in users:\n",
    "    user.set_edge(edge)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:06:45.875264Z",
     "start_time": "2025-02-15T12:03:38.510967Z"
    }
   },
   "outputs": [],
   "source": [
    "hits, misses, bts, fds, onlines = [], [], [], [], []\n",
    "hit_rates = []\n",
    "for t in range(501):\n",
    "    for user in users:\n",
    "        user.step()\n",
    "\n",
    "    edge.step()\n",
    "\n",
    "    hit, miss = edge.cache_hit, edge.cache_miss\n",
    "    fs, fw = edge.first_delay, edge.first_watch\n",
    "    hits.append(hit)\n",
    "    misses.append(miss)\n",
    "    hit_rates.append(0 if (hit + miss) == 0 else hit / (hit + miss))\n",
    "    bts.append(edge.backhaul_traffic)\n",
    "    fds.append(0 if fw == 0 else fs / fw)\n",
    "    onlines.append(edge.online_users)\n",
    "\n",
    "    edge.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5648097826086956"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hit_rates[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.7997152627993291)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean([user.satisfaction() for user in users])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "test",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
